/**
 * FileName: RoleDaoImpl.java
 * 角色Dao实现类
 *
 * @author  王威
 * @Date    2021/11/29
 * @version 1.00
 **/
package com.site.dao.impl;
import com.site.dao.RoleDao;
import com.site.pojo.Employee;
import com.site.pojo.Role;
import com.site.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl implements RoleDao {
    /*查询所有角色*/
    @Override
    public List<Role> selectAll() throws SQLException {
        List<Role> list = new ArrayList<>();
        Connection conn = JDBCUtils.getConnection();
        ResultSet rst = JDBCUtils.executeQuery(conn, "SELECT * FROM `role`");
        while (rst.next()){
            Role role = new Role();
            role.setRid(rst.getLong("rid"));
            role.setRoleName(rst.getString("Role_Name"));
            list.add(role);
        }
        JDBCUtils.close(rst,null,conn);
        return list;
    }

    /*根据id查询角色所有信息*/
    @Override
    public Role selectById(Role role) throws SQLException {
        Role role1 = null;
        Connection conn = JDBCUtils.getConnection();
        ResultSet rst = JDBCUtils.executeQuery(conn, "SELECT * FROM `role` where rid = ?",role.getRid());
        while (rst.next()){
            role1 = new Role();
            role1.setRid(rst.getLong("rid"));
            role1.setRoleName(rst.getString("rolename"));
        }
        JDBCUtils.close(rst,null,conn);
        return role1;
    }

    /*添加角色表数据*/
    @Override
    public int addRole(Connection con, Role role) throws SQLException {
        String sql = "insert into ` hurricane_house `.` ROLE ` (` rid `, ` role_name `)values(?,?);";
        Object[] prmas = {role.getRid(),role.getRoleName()};
        return JDBCUtils.executeUpdate(con,sql,prmas);
    }
    /*删除角色表数据*/
    @Override
    public int delRole(Connection con, Role role) {
        String sql = "delete from ` hurricane_house `.` ROLE ` where` rid ` = ? ;";
        Object[] prmas = {role.getRid()};
        return JDBCUtils.executeUpdate(con,sql,prmas);
    }
    /*修改角色表数据*/
    @Override
    public int updateRole(Connection con, Role role) {
        String sql = "update ` hurricane_house `.` ROLE ` set` rid ` =?, ` role_name ` =? ` state ` = ? where` rid ` =? ;";
        Object[] prmas = {role.getRid(),role.getRoleName(),role.getRid()};
        return JDBCUtils.executeUpdate(con,sql,prmas);
    }
}
